package com.lanvideo.service;

import com.lanvideo.dao.VideoDao;
import com.lanvideo.dao.VideoViewRecordDao;
import com.lanvideo.entity.VideoViewRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 视频观看记录服务
 */
@Service
public class VideoViewRecordService {

    @Autowired
    private VideoViewRecordDao videoViewRecordDao;

    @Autowired
    private VideoDao videoDao;

    /**
     * 记录视频观看
     */
    public void recordVideoView(Integer videoId, Integer userId, Integer watchDuration) {
        // 记录观看记录
        videoViewRecordDao.recordView(videoId, userId, watchDuration);
        
        // 增加视频观看次数
        videoDao.incrementViewCount(videoId);
    }

    /**
     * 获取用户的观看记录
     */
    public List<VideoViewRecord> getUserViewRecords(Integer userId) {
        return videoViewRecordDao.getUserViewRecords(userId);
    }

    /**
     * 获取视频的观看记录
     */
    public List<VideoViewRecord> getVideoViewRecords(Integer videoId) {
        return videoViewRecordDao.getVideoViewRecords(videoId);
    }

    /**
     * 检查用户是否观看过视频
     */
    public boolean hasUserViewed(Integer userId, Integer videoId) {
        return videoViewRecordDao.hasUserViewed(userId, videoId);
    }

    /**
     * 获取用户观看视频的总时长
     */
    public Integer getUserTotalWatchDuration(Integer userId, Integer videoId) {
        return videoViewRecordDao.getUserTotalWatchDuration(userId, videoId);
    }

    /**
     * 获取热门视频
     */
    public List<VideoViewRecord> getPopularVideos(int limit) {
        return videoViewRecordDao.getPopularVideos(limit);
    }

    /**
     * 获取视频观看次数
     */
    public Integer getVideoViewCount(Integer videoId) {
        return videoDao.getViewCount(videoId);
    }
}
